package com.xinxin.intelligenceexam.activity;

import android.app.DatePickerDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Spinner;

import com.xinxin.intelligenceexam.R;
import com.xinxin.intelligenceexam.contract.RegisterContract;
import com.xinxin.intelligenceexam.presenter.RegisterPresenterImpl;
import com.xinxin.intelligenceexam.utils.DateFormatUtil;
import com.xinxin.intelligenceexam.utils.SnackbarUtils;

import java.util.Calendar;
import java.util.Date;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class RegisterActivity extends AppCompatActivity implements RegisterContract.View {


    @BindView(R.id.et_name)
    EditText mEtName;
    @BindView(R.id.et_password)
    EditText mEtPassword;
    @BindView(R.id.et_repassword)
    EditText mEtRePassword;
    @BindView(R.id.et_studentCode)
    EditText mEtStudentCode;
    @BindView(R.id.et_birth)
    EditText mEtBirth;
    @BindView(R.id.spinner_class)
    Spinner mSpinnerClass;
    @BindView(R.id.bt_submit)
    Button mBtSubmit;

    RegisterContract.Presenter mPresenter;
    Date mBirthDate;
    String mClassName = "";
    private Context mContext;
    private String[] mClassNames = null;
    // 定义显示时间控件
    private Calendar calendar; // 通过Calendar获取系统时间
    private int mYear;
    private int mMonth;
    private int mDay;
    private int mHour;
    private int mMinute;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        ButterKnife.bind(this);
        mContext = this;
        mPresenter = new RegisterPresenterImpl();
        mPresenter.attachView(this);
        initView();
        initData();
    }

    @Override
    public void initData() {
        mClassNames = getResources().getStringArray(R.array.java_classes);
        mClassName = mClassNames[0];
        calendar = Calendar.getInstance();
    }

    @Override
    public void initView() {
        mSpinnerClass.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view,
                                       int pos, long id) {

                mClassName = mClassNames[pos];
                //ToastUtils.showShort(mClassName);
                // SnackbarUtils.Short(mBtSubmit, mClassName).info().show();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                // Another interface callback
            }
        });
        mEtRePassword.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                if (b) {
                    mEtPassword.setTextColor(Color.BLACK);
                    mEtRePassword.setTextColor(Color.BLACK);
                } else {
                    if (!checkPassword()) {
                        SnackbarUtils.Short(mBtSubmit, "两次密码不一致").warning().show();
                        mEtPassword.setTextColor(Color.RED);
                        mEtRePassword.setTextColor(Color.RED);
                    }

                }

            }
        });
        mEtBirth.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                if (b) {
                    new DatePickerDialog(mContext,
                            new DatePickerDialog.OnDateSetListener() {
                                @Override
                                public void onDateSet(DatePicker view, int year,
                                                      int month, int day) {
                                    mYear = year;
                                    mMonth = month;
                                    mDay = day;
                                    // 更新EditText控件日期 小于10加0
                                    StringBuilder sb = new StringBuilder()
                                            .append(mYear)
                                            .append("-")
                                            .append((mMonth + 1) < 10 ? "0"
                                                    + (mMonth + 1) : (mMonth + 1))
                                            .append("-")
                                            .append((mDay < 10) ? "0" + mDay : mDay);
                                    try {
                                        mBirthDate = DateFormatUtil.parseStringToDate(sb.toString());
                                    } catch (Exception e) {
                                        e.printStackTrace();
                                    }
                                    mEtBirth.setText(sb.toString());
                                }
                            }, calendar.get(Calendar.YEAR), calendar
                            .get(Calendar.MONTH), calendar
                            .get(Calendar.DAY_OF_MONTH)).show();
                }
            }
        });
    }

    @OnClick({R.id.bt_submit})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.bt_submit:
                String name = mEtName.getText().toString();
                String password = mEtPassword.getText().toString();
                String rePassword = mEtRePassword.getText().toString();
                String studentCode = mEtStudentCode.getText().toString();
                if (TextUtils.isEmpty(name)) {
                    SnackbarUtils.Short(mBtSubmit, "姓名不能为空").warning().show();
                    return;
                }
                if (TextUtils.isEmpty(password) || TextUtils.isEmpty(rePassword)) {
                    SnackbarUtils.Short(mBtSubmit, "密码不能为空").warning().show();
                    return;
                }
                if (TextUtils.isEmpty(studentCode)) {
                    SnackbarUtils.Short(mBtSubmit, "学号不能为空").warning().show();
                    return;
                }
                if (mBirthDate == null) {
                    SnackbarUtils.Short(mBtSubmit, "未选择生日").warning().show();
                    return;
                }
                mPresenter.register(name, password, studentCode, mBirthDate, mClassName);
                break;
        }
    }

    /**
     * @return 密码一致返回真
     */
    private boolean checkPassword() {
        String password = mEtPassword.getText().toString();
        String rePassword = mEtRePassword.getText().toString();
        return !TextUtils.isEmpty(password) && !TextUtils.isEmpty(rePassword) && password.equals(rePassword);
    }

    @Override
    public void snackBarShow(String msg) {
        SnackbarUtils.Short(mBtSubmit, msg).warning().show();
    }

    @Override
    public void gotoMain() {
        Intent intent = new Intent(this, MainActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
        finish();
    }

    @Override
    public void getoLogin() {
        Intent intent = new Intent(this, LoginActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
        finish();
    }

}
